package com.bootcamp.javase.compare.testCat2;

import com.sun.istack.internal.NotNull;

import java.util.Arrays;

/**
 * @program: CompareCode
 * @description:
 * @author: Keyu Li
 * @create: 2021-09-07 21:34
 **/

public class TestCat {
    public static void main(String[] args) {
        Cat[] catSort1 = {new Cat(3, "aa", 2.2),
                new Cat(1, "bbb", 5.6),
                new Cat(2, "cccc", 4.0),
                new Cat(9, "d", 9.9),
                new Cat(5, "abcde", 0)};
        Arrays.sort(catSort1, (o1, o2) -> Double.compare(o2.price, o1.price));
        System.out.println("按照price排序：");
        for (Cat cat : catSort1) {
            System.out.println(cat);
        }
        Arrays.sort(catSort1, (o1, o2) -> o1.name.length() - o2.name.length());
        System.out.println("按照name排序：");
        for (Cat cat : catSort1) {
            System.out.println(cat);
        }


        Cat[] catSort2 = {new Cat(3, "aa", 2.2),
                new Cat(1, "bbb", 5.6),
                new Cat(1, "ccc", 4.0),
                new Cat(1, "d", 9.9),
                new Cat(5, "abcde", 4.0)};
        Arrays.sort(catSort2, TestCat::catCompare);
        System.out.println("综合排序：");
        for (Cat cat : catSort2) {
            System.out.println(cat);
        }

    }

    public static int catCompare(Cat o1, Cat o2) {
        if (o1.age != o2.age) return o1.age - o2.age;
        if (o1.name.length() != o2.name.length()) return o1.name.length() - o2.name.length();
        return Double.compare(o1.price, o2.price);
    }
}

class Cat {
    int age;
    String name;
    double price;

    public Cat( int age, String name, double price) {
        this.age = age;
        this.name = name;
        this.price = price;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("Cat{");
        sb.append("age=").append(age);
        sb.append(", name='").append(name).append('\'');
        sb.append(", price=").append(price);
        sb.append('}');
        return sb.toString();
    }

}
